package com.zzhb.controller;

import java.util.List;

import org.activiti.engine.FormService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.StartFormData;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * helloworld
 *
 */
@Controller
public class ActivitiController {

	private static Logger logger = LoggerFactory.getLogger(ActivitiController.class);

	@Autowired
	RuntimeService rs;

	@Autowired
	TaskService ts;

	@Autowired
	FormService fs;

	@GetMapping("/start/{key}")
	@ResponseBody
	public String start(@PathVariable("key") String key) {
		ProcessInstance processInstance = rs.startProcessInstanceByKey(key);
		System.out.println(processInstance.getId());
		System.out.println(processInstance.getProcessDefinitionId());
		String pId = processInstance.getProcessInstanceId();
		Task task = ts.createTaskQuery().processInstanceId(pId).singleResult();
		logger.info("当前任务名称==" + task.getName());
		return task.getName();
	}

	@GetMapping("/find/{assignee}")
	@ResponseBody
	public String find(@PathVariable("assignee") String assignee) {
		List<Task> list = ts.createTaskQuery().taskAssignee(assignee).list();
		for (Task task : list) {
			System.out.println("id=" + task.getId() + "=assignee=" + task.getAssignee() + "=name=" + task.getName()
					+ "=CreateTime=" + task.getCreateTime());
		}
		return "success";
	}

	@GetMapping("/complete/{taskId}")
	@ResponseBody
	public String complete(@PathVariable("taskId") String taskId) {
		ts.complete(taskId);
		return "success";
	}

	@GetMapping("/form/{pfId}")
	@ResponseBody
	public String form(@PathVariable("pfId") String pfId) {
		StartFormData startFormData = fs.getStartFormData(pfId);
		List<FormProperty> formProperties = startFormData.getFormProperties();
		for (FormProperty formProperty : formProperties) {
			System.out.println("==name==" + formProperty.getName() + "==Type==" + formProperty.getType() + "==Value=="
					+ formProperty.getValue() + "==id==" + formProperty.getId());
		}
		return "success";
	}

}
